package com.github.wxiaoqi.security.biz.modules.community.biz;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.wxiaoqi.security.biz.common.cms.request.CmsTopicCustomerQueryParam;
import com.github.wxiaoqi.security.biz.common.cms.request.CmsTopicCustomerRequestParam;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.biz.modules.community.entity.CmsTopic;
import com.github.wxiaoqi.security.biz.modules.community.entity.CmsTopicCustomer;
import com.github.wxiaoqi.security.biz.modules.community.mapper.CmsTopicCustomerMapper;
import com.github.wxiaoqi.security.biz.modules.community.mapper.CmsTopicMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.ResponseCode;

import lombok.extern.slf4j.Slf4j;

/**
 * 观点关注
 *
 * @author centerroot
 * @email ${email}
 * @date 2018-12-28 09:23:00
 */
@Slf4j
@Service
public class CmsTopicCustomerBiz extends BaseBiz<CmsTopicCustomerMapper,CmsTopicCustomer> {
	
	@Autowired
	private CmsTopicMapper topicMapper;
	
	/**
	 * 添加
	 * @param bean
	 * @return
	 */
	public Map<String, Object> addTopicCustomer(CmsTopicCustomerRequestParam bean){
		 log.info("【添加话题关注】。。。请求参数{}"+bean);
		 CmsTopicCustomer config = new CmsTopicCustomer();
		 Map<String, Object> respMap = new HashMap<>();
		 try {
			 BeanUtils.copyProperties(bean,config);
			 CmsTopicCustomer configReq = new CmsTopicCustomer();
			 configReq.setPlatformId(bean.getPlatformId());
			 configReq.setTopicId(bean.getTopicId());
			 configReq.setCustomerId(bean.getCustomerId());
			 CmsTopicCustomer configRes = mapper.selectOne(configReq);
			 if (null != configRes) {
				configRes.setFollowStatus("0");
				configRes.setUpdateTime(new Date());
				mapper.updateByPrimaryKeySelective(configRes);
			}else {
				CmsTopic topicReq = new CmsTopic();
				topicReq.setId(bean.getTopicId());
				CmsTopic topic = topicMapper.selectOne(topicReq);
				if (null != topic) {
					config.setCreateTime(new Date());
					config.setFollowStatus("0");
					mapper.insert(config);
				}else {
					respMap.put("code", BizResponseCode.FUNCTION_NO_EXIST.getCode());
					respMap.put("message", BizResponseCode.FUNCTION_NO_EXIST.getMessage());
					return respMap;
				}
			}
			respMap.put("code", ResponseCode.OK.getCode());
			respMap.put("message", ResponseCode.OK.getMessage());
		 } catch (Exception e) {
			e.printStackTrace();
			log.info("【添加话题关注】  报错：" + e.getMessage());
			respMap.put("code", BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			respMap.put("message", BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return respMap;
		 }
		 log.info("【添加话题关注】   响应参数：{}", respMap);
		 return respMap;
	}
	
	
	/**
	 * 修改
	 * @param bean
	 * @return
	 */
	public Map<String, Object> updateTopicCustomer(CmsTopicCustomerRequestParam bean){
		 log.info("【修改话题关注】。。。请求参数{}"+bean);
		 CmsTopicCustomer config = new CmsTopicCustomer();
		 Map<String, Object> respMap = new HashMap<>();
		 try {
			 BeanUtils.copyProperties(bean,config);
			 CmsTopicCustomer configReq = new CmsTopicCustomer();
			 configReq.setPlatformId(bean.getPlatformId());
			 configReq.setTopicId(bean.getTopicId());
			 configReq.setCustomerId(bean.getCustomerId());
			 CmsTopicCustomer configRes = mapper.selectOne(configReq);
			 if (null != configRes) {
				 configRes.setFollowStatus("1");
				 configRes.setUpdateTime(new Date());
				 if (mapper.updateByPrimaryKeySelective(configRes) > 0) {
					 respMap.put("code", ResponseCode.OK.getCode());
					 respMap.put("message", ResponseCode.OK.getMessage());
				 }
			}else {
				respMap.put("code", BizResponseCode.FUNCTION_NO_EXIST.getCode());
				respMap.put("message", BizResponseCode.FUNCTION_NO_EXIST.getMessage());
				return respMap;
			}
		 } catch (Exception e) {
			e.printStackTrace();
			log.info("【修改话题关注】  报错：" + e.getMessage());
			respMap.put("code", BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			respMap.put("message", BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return respMap;
		 }
		 log.info("【修改话题关注】   响应参数：{}", respMap);
		 return respMap;
	}
	
	/**
	 * 查询关注话题
	 * @param param
	 * @return
	 */
	public Map<String, Object> followTopics(CmsTopicCustomerQueryParam param){
		 log.info("【查询关注话题】。。。请求参数{}"+param);
		 Map<String, Object> respMap = new HashMap<>();
		 try {
			 Map<String, Object> reqMap = new HashMap<>();
			 reqMap.put("platformId", param.getPlatformId());
			 reqMap.put("customerId", param.getCustomerId());
			 reqMap.put("page", param.getPage());
			 reqMap.put("limit", param.getLimit());
			 List<CmsTopic> list = topicMapper.queryFollowTopics(reqMap);
			 respMap.put("code", BizResponseCode.OK.getCode());
			 respMap.put("message", BizResponseCode.OK.getMessage());
			 respMap.put("topics", list);
		 } catch (Exception e) {
			e.printStackTrace();
			log.info("【修改话题关注】  报错：" + e.getMessage());
			respMap.put("code", BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			respMap.put("message", BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return respMap;
		 }
		 log.info("【查询关注话题】   响应参数：{}", respMap);
		 return respMap;
	}
	
}